package com.ruoyi.asset.sorting.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.asset.sorting.domain.bo.GarbageSortingOverviewBo;
import com.ruoyi.asset.sorting.domain.bo.GarbageSortingSearchBo;
import com.ruoyi.asset.sorting.domain.bo.StatisticianSortingByRegion;
import com.ruoyi.asset.sorting.domain.vo.GarbageSortingVo;
import com.ruoyi.asset.sorting.domain.vo.SortingCommunityCountVo;
import com.ruoyi.asset.sorting.domain.vo.SortingCommunityMapVo;
import com.ruoyi.asset.sorting.domain.vo.SortingWorkStatisticsVo;
import com.ruoyi.asset.sorting.service.IGarbageSortingService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 垃圾分类督导
 *
 * @author lq
 * @date 2023-11-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/asset/sorting")
public class GarbageSortingController extends BaseController {

    private final IGarbageSortingService garbageSortingService;

    /**
     * 获取垃圾分类督导-管理范围导入模板
     */
    @SaIgnore()
    @Log(title = "垃圾分类督导管理范围导入", businessType = BusinessType.IMPORT)
    @RepeatSubmit()
    @PostMapping(value = "/importData")
    public R<List<String>> importData(@RequestParam(name = "excelUrl") String excelUrl) {
        List<String> list = this.garbageSortingService.importExcel(excelUrl);

        if (list == null) {
            return R.fail();
        } else {
            return R.ok(list);
        }
    }

    /**
     * 获取垃圾分类督导-管理范围导入模板
     */
    @SaIgnore()
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "垃圾分类督导管理范围导入模板", GarbageSortingVo.class, response);
    }

    /**
     * 删除垃圾分类督导管理范围
     */
    @SaIgnore()
    @DeleteMapping("/deleteSorting/{ids}")
    public R<Void> deleteMaintenanceScope(@NotEmpty(message = "主键不能为空")
                                          @PathVariable Set<Long> ids) {
        int count = this.garbageSortingService.deleteMaintenanceScope(ids);

        return toAjax(count >= 0 ? 1 : count);
    }

    /**
     * 查询垃圾分类督导管理范围
     */
    @SaIgnore()
    @GetMapping("/querySorting")
    public TableDataInfo<GarbageSortingVo> queryMaintenanceScope(GarbageSortingSearchBo searchBo, PageQuery pageQuery) {
        return this.garbageSortingService.queryMaintenanceScope(searchBo, pageQuery);
    }

    /**
     * 导出垃圾分类督导管理范围
     */
    @SaIgnore()
    @Log(title = "垃圾分类督导导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSorting")
    public void exportMaintenanceScope(GarbageSortingSearchBo searchBo, HttpServletResponse response) {
        List<GarbageSortingVo> list = this.garbageSortingService.queryMaintenanceScope(searchBo);

        ExcelUtil.exportExcel(list, "垃圾分类督导管理范围", GarbageSortingVo.class, response);
    }

    /**
     * 按区统计垃圾分类
     */
    @SaIgnore()
    @GetMapping("/statForSortingByRegion")
    public R<List<StatisticianSortingByRegion>> statForSortingByRegion() {
        List<StatisticianSortingByRegion> stat = this.garbageSortingService.statForSortingByRegion();

        return R.ok(stat);
    }

    /**
     * 按区统计垃圾分类
     */
    @SaIgnore()
    @GetMapping("/garbageSortingCommunityCount")
    public R<Map> garbageSortingCommunityCount() {
        Map map = this.garbageSortingService.getGarbageSortingCommunityCount();
        return R.ok(map);
    }

    @SaIgnore()
    @GetMapping("/sortingCount")
    public R<GarbageSortingOverviewBo> sortingCount() {
        GarbageSortingOverviewBo garbageSortingOverview = this.garbageSortingService.getGarbageSortingOverview();
        return R.ok(garbageSortingOverview);
    }

    /**
     * 查询智慧大屏垃圾分类督导玉塘街道全社区
     *
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/sortingCommunity")
    public R<List<SortingCommunityCountVo>> sortingCommunity(String date) {
        date = "2024-03-13";
        List<SortingCommunityCountVo> sortingCountByCommunity = garbageSortingService.getSortingCountByCommunity(date);
        return R.ok(sortingCountByCommunity);
    }

    /**
     * 查询智慧大屏垃圾分类督导今日作业统计与百分比
     *
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/sortingWorkByDate")
    public R<SortingWorkStatisticsVo> sortingWorkByDate(String date) {
        date = "2024-03-13";
        SortingWorkStatisticsVo sortingWorkByDate = garbageSortingService.getSortingWorkByDate(date);
        return R.ok(sortingWorkByDate);
    }

    /**
     * 查询智慧大屏垃圾分类督导今日作业统计与百分比
     * 指定社区
     * @param date 日期
     * @param community 社区
     * @return
     */
    @SaIgnore()
    @GetMapping("/sortingWorkByDateAndCommunity")
    public R<SortingWorkStatisticsVo> sortingWorkByDateAndCommunity(String date,String community) {
        date = "2024-03-13";
        SortingWorkStatisticsVo sortingWorkByDate = garbageSortingService.getSortingWorkByDateAndCommunity(date,community);
        return R.ok(sortingWorkByDate);
    }

    /**
     * 查询智慧大屏垃圾分类督导按社区区分
     *
     * @param community
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/communityByDate")
    public R<SortingCommunityMapVo> communityByDate(String community, String date) {
        date = "2024-03-13";
        SortingCommunityMapVo communityByDate = garbageSortingService.getCommunityByDate(community, date);
        return R.ok(communityByDate);
    }
}
